package cn.edu.cug.cs.gtl.renders.openinventor.viewers;

import cn.edu.cug.cs.gtl.renders.openinventor.canvas.RenderAreaExaminerImpl;
import cn.edu.cug.cs.gtl.apis.render.canvas.RenderAreaExaminer;
import cn.edu.cug.cs.gtl.apis.render.nodes.Node;
import cn.edu.cug.cs.gtl.apis.render.viewers.Viewer;
import com.openinventor.inventor.actions.SoBoxHighlightRenderAction;
import com.openinventor.inventor.nodes.SoNode;
import com.openinventor.inventor.viewercomponents.awt.glcanvas.viewers.ViewerExaminer;

import javax.swing.*;
import java.awt.*;

public class ViewerImpl implements Viewer {
    protected ViewerExaminer nativeObject=null;
    public ViewerImpl() {
        super();
        this.nativeObject=new ViewerExaminer();
        this.nativeObject.getRenderArea().setGLRenderAction(new SoBoxHighlightRenderAction());
    }

    public ViewerImpl(ViewerExaminer v){
        this.nativeObject = v;
        this.nativeObject.getRenderArea().setGLRenderAction(new SoBoxHighlightRenderAction());
    }

    @Override
    public Object getNativeObject() {
        return nativeObject;
    }

    @Override
    public boolean attach(Object nativeObject) {
        if(nativeObject instanceof ViewerExaminer)
            this.nativeObject = (ViewerExaminer)nativeObject;
        else
            return false;
        return false;
    }

    @Override
    public Object detach() {
        ViewerExaminer v=  this.nativeObject;
        this.nativeObject=null;
        return v;
    }

    @Override
    public void viewAll() {
        this.nativeObject.viewAll();
    }

    @Override
    public JPanel getPanel() {
        if(this.nativeObject==null)
            return null;
        if(this.nativeObject instanceof JPanel)
            return (JPanel)this.nativeObject;
        else
            return null;
    }

    @Override
    public Component getComponent() {
        if(this.nativeObject==null)
            return null;
        if(this.nativeObject instanceof Component)
            return (Component)this.nativeObject;
        else
            return null;
    }

    @Override
    public RenderAreaExaminer getRenderArea() {
        return new RenderAreaExaminerImpl(this.nativeObject.getRenderArea());
    }

    @Override
    public void setSceneGraph(Node node) {
        this.nativeObject.setSceneGraph((SoNode)(node.getNativeObject()));
    }


}
